package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.*;
import cn.wolfcode.trip.base.mapper.DailyContentMapper;
import cn.wolfcode.trip.base.mapper.DailyMapper;
import cn.wolfcode.trip.base.query.DailyQuery;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IDailyService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyServiceImpl implements IDailyService {
    @Autowired
    private DailyMapper dailyMapper;
    @Autowired
    private DailyContentMapper dailyContentMapper;

    @Override
    public PageInfo query(DailyQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(), qo.getOrderBy());
        List<Strategy> list = dailyMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public DailyContent getContentById(Long id) {
        return dailyContentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void saveOrUpdate(Daily entity) {
        //获取日报文章对象
        DailyContent dailyContent = entity.getDailyContent();
        if (entity.getId() != null) {
            //修改
            //更新日报表
            dailyMapper.updateByPrimaryKey(entity);
            //更新日报内容表
            dailyContent.setId(entity.getId());
            dailyContentMapper.updateByPrimaryKey(dailyContent);
        } else {
            //新增
            entity.setCreateTime(new Date());
            //保存日报文章
            dailyMapper.insert(entity);
            //更新文章内容表
            dailyContent.setId(entity.getId());
            dailyContentMapper.insert(dailyContent);
        }
    }

    @Override
    public void delete(Long id) {
        dailyMapper.deleteByPrimaryKey(id);
        dailyContentMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Daily getById(Long id) {
        return dailyMapper.selectByPrimaryKey(id);
    }
}
